var express = require('express');
var router = express.Router();
var User = require('../data/models/user');
var Product = require('../data/models/product');
var middleware = require('../middleware/middleware');

//商品发布
router.get('/publish',middleware.checkLogin);
router.get('/publish',function(req,res) {
    res.render('publish',{
        title: "publish"
    });
});
router.post('/publishSucc',middleware.checkLogin);
router.post('/publishSucc',function(req,res) {
    var _product = new Product({
        username: req.session.user,
        name: req.body.name,
        description: req.body.description,
        catagory: req.body.catagory,
        price: req.body.price
    });
    _product.save(function(err) {
        if(err) {
            console.log(err);
        }
        res.redirect('/users/'+req.session.user);
    });

});

//商品删除
router.delete('/delProduct',middleware.checkLogin);
router.delete('/delProduct',function(req,res) {
    var prodId = req.query.prodId;
    Product.remove({_id: prodId},function(err) {
        if(err) {
            console.log(err);
        }
        res.json({success:1});
    });
});

//商品修改
router.get('/putProudct',middleware.checkLogin);
router.get('/putProudct',function(req,res) {

});

module.exports = router;
